package org.osomit.sacct.entity;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.stringtree.json.JSONReader;
import org.osomit.sacct.builder.RequestBuilder;
import org.osomit.sacct.security.utils.SecurityUtil;
import org.osomit.sacct.security.utils.SharedSecurityGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 28, 2009
 */
public class Request_UT {

    @Test
    public void testToJSON(){
        Request req = new Request();
        req.setRnd(SecurityUtil.base64Encode(SharedSecurityGenerator.generateSharedSecret(64)));
        req.setCommand("handshake");
        RequestContext context = new RequestContext();
        context.setLocale("us_EN");
        context.setClientId("test");
        req.setRequestContext(context);
        String clientPubKey = "3121513513";
        List list = new ArrayList();
        list.add(clientPubKey);
        req.setParameters(list);

        String str = req.toJSON().toString();
        System.out.println("Request: " + str);
    }

    @Test
    public void testBuildObjectFromJSONString(){
        Request req = new Request();
        req.setRnd(SecurityUtil.base64Encode(SharedSecurityGenerator.generateSharedSecret(64)));
        req.setCommand("handshake");
        RequestContext context = new RequestContext();
        context.setLocale("us_EN");
        context.setClientId("test");
        req.setRequestContext(context);
        String clientPubKey = "3121513513";
        List list = new ArrayList();
        list.add(clientPubKey);
        req.setParameters(list);

        String str = req.toJSON().toString();
        JSONReader jsreader = new JSONReader();
        Object in = jsreader.read(str);
        RequestBuilder builder = new RequestBuilder();
        Request request = (Request)builder.build(in);
        assertEquals("handshake", request.getCommand());
        assertEquals("us_EN", request.getRequestContext().getLocale());
        assertEquals("test", request.getRequestContext().getClientId());
        List params = request.getParameters();
        assertNotNull(params);

        assertEquals("3121513513", params.get(0));
    }
}
